package cn.fivestars.springcloud.order.controller;

import cn.fivestars.springcloud.commons.entities.CommonResult;
import cn.fivestars.springcloud.commons.entities.Payment;
import cn.fivestars.springcloud.order.lb.LoadBalancer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * OrderController
 *
 * @author liangchenzhou1024@163.com
 * @date 2021/2/1
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {


    // public static final String PAYMENT_URL = "http://localhost:8001";
    public static final String CLOUD_PAYMENT_SERVICE = "cloud-payment-service";
    public static final String CLOUD_PAYMENT_SERVICE_CONTEXT = "/cloud-payment-service";

    public static final String PAYMENT_URL = "http://" + CLOUD_PAYMENT_SERVICE + CLOUD_PAYMENT_SERVICE_CONTEXT;

    @Resource
    private RestTemplate restTemplate;

    //@PostMapping("/payment/")
    //public CommonResult<Payment> save(@RequestBody Payment payment) {
    //    return restTemplate.postForObject(PAYMENT_URL + "/payment/", payment, CommonResult.class);
    //}

    @PostMapping("/payment/")
    public CommonResult<Payment> save(@RequestBody Payment payment) {
        ResponseEntity<CommonResult> response = restTemplate.postForEntity(PAYMENT_URL + "/payment/", payment, CommonResult.class);
        if (response.getStatusCode().is2xxSuccessful()) {
            return response.getBody();
        } else {
            return CommonResult.failure(444, "error");
        }
    }


    //@GetMapping("/payment/{id}")
    //public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
    //    return restTemplate.getForObject(PAYMENT_URL + "/payment/" + id, CommonResult.class);
    //}

    @GetMapping("/payment/{id}")
    public CommonResult<Object> getPayment(@PathVariable("id") Long id) {
        ResponseEntity<CommonResult> response = restTemplate.getForEntity(PAYMENT_URL + "/payment/" + id, CommonResult.class);
        if (response.getStatusCode().is2xxSuccessful()) {
            return response.getBody();
        } else {
            return CommonResult.failure(444, "error");
        }
    }

    @GetMapping("/payment/server")
    public CommonResult<String> server() {
        return restTemplate.getForObject(PAYMENT_URL + "/payment/server", CommonResult.class);
    }

    @Resource
    private LoadBalancer loadBalancer;

    @Resource
    private DiscoveryClient discoveryClient;

    /**
     * 注意: 使用该方法调用, restTemplate 需要把 Ribbon负载均衡 关闭, 否则 restTemplate 无法通过uri的方式调用
     *
     * @return CommonResult
     */
    @GetMapping("/lb")
    public CommonResult<String> myLoadBalancer() {
        List<ServiceInstance> instances = discoveryClient.getInstances(CLOUD_PAYMENT_SERVICE);
        if (instances == null || instances.size() == 0) {
            return CommonResult.failure(444, "no server");
        }

        ServiceInstance server = loadBalancer.getInstance(instances);
        String host = server.getUri().toString();
        log.info("host={}", host);
        return restTemplate.getForObject(host + "/payment/server", CommonResult.class);
    }


}
